package com.ruoyi.im.service;

import com.ruoyi.im.entity.*;
import com.ruoyi.im.utils.RouteQueueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String GROUP_KEY_ID_INCRE = "ws:group:id:max";
    /**
     * 群组的用户
     */
    private static final String GROUP_INFO = "ws:group:id:";
    /**
     * 指定用户的好友信息
     */
    private static final String FRIEND_INFO_USER = "ws:friend:user:";

    private static final String TABLE_USER_INFO_KEY = "table:user:info";
    /**
     * 群组
     * GroupInfoEntity
     */
    private static final String TABLE_USER_GROUP_KEY = "table:user:group:";

    @Autowired
    private RouteQueueUtil routeQueueUtil;

    @Autowired
    private MsgService msgService;

    public Long createGroup(GroupEntity entity) {
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        Long increment = vo.increment(GROUP_KEY_ID_INCRE);
        entity.setGroupId(increment.intValue());

        ListOperations<String, Object> ops = redisTemplate.opsForList();
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();

        String name = "群组" + increment;//默认群组的名称
        GroupInfoEntity entity1 = new GroupInfoEntity();
        entity1.setGroupName(name);
        entity1.setCreateUserId(entity.getCreateUserId());
        entity1.setGroupName(name);
        entity1.setGroupId(increment.intValue());

        for (Integer userId : entity.getUserId()) {
            Object o = opsForHash.get(TABLE_USER_INFO_KEY, userId + "");
            ops.rightPush(GROUP_INFO + increment, o);
            //为每个用户添加群组
            ops.rightPush(TABLE_USER_GROUP_KEY + userId, entity1);
        }

        return increment;
    }

    public void delGroup(Integer groupId, Integer userId) {
        //删除群组
        String key = TABLE_USER_GROUP_KEY + userId;
        redisTemplate.delete(key);
        //删除群组信息
        String key1 = GROUP_INFO + groupId;
        redisTemplate.delete(key1);
    }

    public List<Object> getGroupUserList(Integer groupId) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        List<Object> range = ops.range(GROUP_INFO + groupId, 0, -1);
        return range;
    }

    /**
     * 查询我的好友
     *
     * @return
     */
    public List<Object> queryFriend(Integer userId) {
        ListOperations<String, Object> olist = redisTemplate.opsForList();
        List<Object> range = olist.range(FRIEND_INFO_USER + userId, 0, -1);
        return range;
    }

    /**
     * 接收人同意添加为好友
     *
     * @param userId         添加人
     * @param receivedUserId 接收人
     * @return
     */
    public int addFriend(Integer userId, Integer receivedUserId) {
        //查询用户的数据
        HashOperations<String, Object, Object> hashop = redisTemplate.opsForHash();
        UserEntity u1 = (UserEntity) hashop.get(TABLE_USER_INFO_KEY, userId + "");
        UserEntity u2 = (UserEntity) hashop.get(TABLE_USER_INFO_KEY, receivedUserId + "");

        //建立好友关系
        //a==>b
        ListOperations<String, Object> olist = redisTemplate.opsForList();
        olist.rightPush(FRIEND_INFO_USER + userId, u2);
        //b==>a
        olist.rightPush(FRIEND_INFO_USER + receivedUserId, u1);
        return 1;
    }

    public void addFriendReq(Integer userId, Integer receivedUserId, String msg) {
        //通过会话发送消息
        MsgRoute route = new MsgRoute();
        route.setReceiverId(receivedUserId);
        route.setMsgContent(msg);
        route.setType(1);
        route.setMsgCate(2);
        route.setSenderId(userId);
        route.setMsgType(1);
        route.setMsgId(msgService.geneMsgId(receivedUserId, userId));
        routeQueueUtil.msgToPerson(route);
        //todo 保存消息到db中，用于接口查询
    }

    public List<Object> getUserGroup(Integer userId) {
        //获取用户的群组信息
        ListOperations<String, Object> lop = redisTemplate.opsForList();
        //自己创建的组
        List<Object> range = lop.range(TABLE_USER_GROUP_KEY + userId, 0, -1);
        //自己加入的群组

        return range;
    }

    /**
     * 注册一个用户
     *
     * @param entity
     * @return
     */
    public String registerUser(UserEntity entity) {
        //生成用户id
        Integer id = msgService.geneUserId();
        entity.setUserId(id);
        //保存用户数据
        HashOperations<String, Object, Object> hashOp = redisTemplate.opsForHash();
        hashOp.put(TABLE_USER_INFO_KEY, id + "", entity);
        return null;
    }

    public Object userInfo(Integer userId) {
        HashOperations<String, Object, Object> hashOp = redisTemplate.opsForHash();
        return hashOp.get(TABLE_USER_INFO_KEY, userId + "");
    }

    public Integer geneMsgId(MsgIdEntity entity) {
        Integer integer = msgService.geneMsgId(entity.getReceiverId(), entity.getSenderId());
        return integer;
    }
}
